{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Remove k-th Last Element From Linked List\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/uPzWlWqfO4gj8BXgP1XXoA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "You are given a singly linked list and an integer k. Return the linked list, removing the k-th last element from the list.  \n",
    "\n",
    "Try to do it in a single pass and using constant space."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[1, 2, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "class Node:\n",
    "    \n",
    "    def __init__(self, val, next=None):\n",
    "        self.val = val\n",
    "        self.next = next\n",
    "        pass\n",
    "\n",
    "    def __str__(self):\n",
    "        current_node = self\n",
    "        result = list()\n",
    "        while current_node is not None:\n",
    "            result.append(current_node.val)\n",
    "            current_node = current_node.next\n",
    "            pass\n",
    "        return str(result)\n",
    "\n",
    "    pass\n",
    "\n",
    "\n",
    "def remove_kth_from_linked_list(head, k):\n",
    "    tail = head\n",
    "    for i in range(k):\n",
    "        tail = tail.next\n",
    "        pass\n",
    "    deletion = head\n",
    "    while tail.next is not None:\n",
    "        deletion = deletion.next\n",
    "        tail = tail.next\n",
    "        pass\n",
    "    deletion.next = deletion.next.next\n",
    "    return head\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    head = Node(1, Node(2, Node(3, Node(4, Node(5)))))\n",
    "    print(head)\n",
    "    head = remove_kth_from_linked_list(head, 3)\n",
    "    print(head)\n",
    "    pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
